<?php
class Url{
	static function url_exists($url){
		/* checks if a page exists*/
		$handle = @fopen($url,'r');
		if($handle !== false){ return true;}  
		else {return false;}
	}
    
	static function remove_urls_not_in_domain($urls,$domain){

		$cleanedurls = array();
		foreach ($urls as $url){
			$length_domain = strlen($domain);
			$possibledomain= substr($url,0,$length_domain);
			if($possibledomain == $domain){array_push($cleanedurls,$url);}
		}
              
		return $cleanedurls;
	}
        
        static function removeParameters($url){
            $pos = strpos ($url,'?');
            if ($pos !== false){
                return substr($url,0,$pos);
            }
            else{
            return $url;
            }
        }

	static function relative_to_abolute_url($absolute, $relative) { // returns an absolute path for a relative url
            $givenurl = $relative;
            $p = parse_url($relative,PHP_URL_SCHEME);
        if(!$p == NULL){return $relative;}
        extract(parse_url($absolute));
        $path = dirname($path); 
        
        if(substr($relative,0,1) == '/'){$cparts = array_filter(explode("/", $relative));}
        else {
            $aparts = array_filter(explode("/", $path));
            $rparts = array_filter(explode("/", $relative));
            $cparts = array_merge($aparts, $rparts);
            foreach($cparts as $i => $part) {
                if($part == '.'){$cparts[$i] = null;}
                if($part == '..'){$cparts[$i - 1] = null; $cparts[$i] = null;}
            }
            $cparts = array_filter($cparts);
        }
        $path = implode("/", $cparts);
        $url = "";
        if($scheme){$url = "$scheme://";}
        if(isset($user)){
            $url .= "$user";
            if($pass){$url .= ":$pass";}
            $url .= "@";
        }
        if($host){$url .= "$host/";}
        $url .= $path;
        //echo $givenurl."=>".$url."<br />";
        return $url;
	}

}
?>